package com.feng.gameproj;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;

public class BitmapCtrlView extends View implements Runnable 
{
	private static Resources res = null;
	
	private Bitmap dealBitmap = null;
	private Bitmap displayBitmap = null;
	
	private Matrix metrix = null;
	
	public float angle = 0.0F;
	public float scale = 0.5F;

	public BitmapCtrlView(Context context, int bitmapID) 
	{
		super(context);
		res = getResources ();
		BitmapDrawable tmp = (BitmapDrawable)res.getDrawable(bitmapID);
		this.dealBitmap = tmp.getBitmap();
		this.metrix = new Matrix ();
		
		new Thread (this).start();
	}

	@Override
	protected void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas);
		
		this.metrix.reset();
		this.metrix.setRotate(this.angle);
		this.metrix.postScale(this.scale, this.scale);
		
		int bitmapHeight = this.dealBitmap.getHeight();
		int bitmapWidth = this.dealBitmap.getWidth();
		System.out.println ("Height + Width : " + bitmapHeight + ", " + bitmapWidth);
		this.displayBitmap = Bitmap.createBitmap(this.dealBitmap, 
				0, 0, bitmapHeight, bitmapWidth, this.metrix, true);
		canvas.drawBitmap(this.displayBitmap, 0, 0, new Paint ());
		
		this.displayBitmap = null;
	}

	@Override
	public void run() 
	{
		while (! Thread.currentThread().isInterrupted())
		{
			try 
			{
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Thread.currentThread().interrupt();
			}
			postInvalidate ();
		}
	}

}
